package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultData;
import com.net.xpay.admin.vo.CompanyDailyStatVo;
import com.net.xpay.common.domain.stat.CompanyRealTimeStat;
import com.net.xpay.common.manager.stat.CompanyRealTimeStatManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;

@RestController
@RequestMapping("/admin/company-real-time-stat")
@Api(tags = "运营/后台经营数据实时统计", description = "运营 后台经营数据实时统计 net")
public class AdminCompanyRealTimeStatController {

    @Autowired
    private CompanyRealTimeStatManager companyRealTimeStatManager;

//    @ApiOperation("实时统计")
//    @PostMapping("/select")
//    @ResponseBody
//    @PreAuthorize("hasAnyRole('ADMIN', 'company-real-time-stat::select')")
//    public ResultData<CompanyDailyStatVo> selectCompanyRealTimeStat() {
//        CompanyDailyStatVo vo = new CompanyDailyStatVo();
//
//        CompanyRealTimeStat companyRealTimeStat = companyRealTimeStatManager.getById(1L);
//        if (companyRealTimeStat == null) {
//            return new ResultData<>(vo);
//        }
//
//        //计算信息
//        //平均费率  服务费收入/付款成功金额
//        String averageRate = null;
//        if (companyRealTimeStat.getTotalSuccessTransAmount().compareTo(new BigDecimal("0")) != 0) {
//            averageRate = companyRealTimeStat.getTotalServicePrice().multiply(new BigDecimal("100")).divide(companyRealTimeStat.getTotalSuccessTransAmount(), 2, RoundingMode.HALF_UP).toString();
//        }
//
//
//        //批量有密支付成功率
//        String payTransSuccessRate = null;
//        if (companyRealTimeStat.getPayTransCount() != 0L) {
//            payTransSuccessRate = new BigDecimal(companyRealTimeStat.getPayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyRealTimeStat.getPayTransCount()), 2, RoundingMode.HALF_UP).toString();
//        }
//
//
//        //安全发支付成功率
//        String safePaySuccessRate = null;
//        if (companyRealTimeStat.getSafePayTransCount() != 0L){
//            safePaySuccessRate = new BigDecimal(companyRealTimeStat.getSafePayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyRealTimeStat.getSafePayTransCount()), 2, RoundingMode.HALF_UP).toString();
//        }
//
//
//        vo.setAverageRate(averageRate);
//        vo.setPayTransSuccessRate(payTransSuccessRate);
//        vo.setSafePaySuccessRate(safePaySuccessRate);
//
//        vo.setUpdateTime(companyRealTimeStat.getUpdateTime());
//        vo.setRegisterCount(companyRealTimeStat.getRegisterCount());
//        vo.setServiceChargeFee(companyRealTimeStat.getServiceChargeFee());
//        vo.setTotalCount(companyRealTimeStat.getTotalCount());
//        vo.setTotalSuccessCount(companyRealTimeStat.getTotalSuccessCount());
//        vo.setTotalFailCount(companyRealTimeStat.getTotalFailCount());
//        vo.setTotalSuccessSingleVeryCount(companyRealTimeStat.getTotalSuccessSingleVeryCount());
//        vo.setTotalFailSingleVeryCount(companyRealTimeStat.getTotalFailSingleVeryCount());
//        vo.setTotalServicePrice(companyRealTimeStat.getTotalServicePrice());
//        vo.setTotalTransAmount(companyRealTimeStat.getTotalTransAmount());
//        vo.setTotalSuccessTransAmount(companyRealTimeStat.getTotalSuccessTransAmount());
//        vo.setTotalFailTransAmount(companyRealTimeStat.getTotalFailTransAmount());
//        vo.setTotalBenefit(companyRealTimeStat.getTotalBenefit());
//        vo.setPayTransCount(companyRealTimeStat.getPayTransCount());
//        vo.setPayTransSuccessCount(companyRealTimeStat.getPayTransSuccessCount());
//        vo.setPayTransFailCount(companyRealTimeStat.getPayTransFailCount());
//
//        vo.setPayTransSuccessSingleVeryCount(companyRealTimeStat.getPayTransSuccessSingleVeryCount());
//        vo.setPayTransFailSingleVeryCount(companyRealTimeStat.getPayTransFailSingleVeryCount());
//        vo.setPayTransAmount(companyRealTimeStat.getPayTransAmount());
//        vo.setPayTransSuccessAmount(companyRealTimeStat.getPayTransSuccessAmount());
//        vo.setPayTransFailAmount(companyRealTimeStat.getPayTransFailAmount());
//        vo.setPayTransServicePrice(companyRealTimeStat.getPayTransServicePrice());
//        vo.setTotalPayTransBenefit(companyRealTimeStat.getTotalPayTransBenefit());
//        vo.setSafePayTransCount(companyRealTimeStat.getSafePayTransCount());
//        vo.setSafePayTransSuccessCount(companyRealTimeStat.getSafePayTransSuccessCount());
//        vo.setSafePayTransFailCount(companyRealTimeStat.getSafePayTransFailCount());
//        vo.setSafePayTransVeryAlipayUserCount(companyRealTimeStat.getSafePayTransVeryAlipayUserCount());
//
//        vo.setSafePayTransFailSingleVeryCount(companyRealTimeStat.getSafePayTransFailSingleVeryCount());
//        vo.setSafePayTransAmount(companyRealTimeStat.getSafePayTransAmount());
//        vo.setSafePayTransSuccessAmount(companyRealTimeStat.getSafePayTransSuccessAmount());
//        vo.setSafePayTransFailAmount(companyRealTimeStat.getSafePayTransFailAmount());
//        vo.setSafePayTransServicePrice(companyRealTimeStat.getSafePayTransServicePrice());
//        vo.setTotalSafePayTransBenefit(companyRealTimeStat.getTotalSafePayTransBenefit());
//
//
//        vo.setPayTransActiveUserCount(companyRealTimeStat.getPayTransActiveUserCount());
//        vo.setPayTransActivePartnerCount(companyRealTimeStat.getPayTransActivePartnerCount());
//
//        vo.setSafePayActiveUserCount(companyRealTimeStat.getSafePayActiveUserCount());
//        vo.setSafePayActivePartnerCount(companyRealTimeStat.getSafePayActivePartnerCount());
//
//        vo.setSafePayPayerAlipayAccountCount(companyRealTimeStat.getSafePayPayerAlipayAccountCount());
//        vo.setSafePayPayeeAlipayAccountCount(companyRealTimeStat.getSafePayPayeeAlipayAccountCount());
//
//        vo.setPayTransPayerAlipayAccountCount(companyRealTimeStat.getPayTransPayerAlipayAccountCount());
//        vo.setPayTransPayeeAlipayAccountCount(companyRealTimeStat.getPayTransPayeeAlipayAccountCount());
//
//        vo.setSafePayTransSuccessSingleVeryCount(companyRealTimeStat.getSafePayTransSuccessSingleVeryCount());
//        vo.setPayTransVeryAlipayUserCount(companyRealTimeStat.getPayTransVeryAlipayUserCount());
//
//
//        return new ResultData<>(vo);
//    }

    //todo 数据处理
    @ApiOperation("实时统计")
    @PostMapping("/select")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'company-real-time-stat::select')")
    public ResultData<CompanyDailyStatVo> selectCompanyRealTimeStat() {
        CompanyDailyStatVo vo = new CompanyDailyStatVo();

        CompanyRealTimeStat companyRealTimeStat = companyRealTimeStatManager.getById(1L);
        if (companyRealTimeStat == null) {
            return new ResultData<>(vo);
        }

        //计算信息
        //平均费率  服务费收入/付款成功金额
        String averageRate = null;
        if (companyRealTimeStat.getTotalSuccessTransAmount().compareTo(new BigDecimal("0")) != 0) {
            averageRate = companyRealTimeStat.getTotalServicePrice().multiply(new BigDecimal("100")).divide(companyRealTimeStat.getTotalSuccessTransAmount(), 2, RoundingMode.HALF_UP).toString();
        }


        //批量有密支付成功率
        String payTransSuccessRate = null;
        if (companyRealTimeStat.getPayTransCount() != 0L) {
            payTransSuccessRate = new BigDecimal(companyRealTimeStat.getPayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyRealTimeStat.getPayTransCount()), 2, RoundingMode.HALF_UP).toString();
        }


        //安全发支付成功率
        String safePaySuccessRate = null;
        if (companyRealTimeStat.getSafePayTransCount() != 0L){
            safePaySuccessRate = new BigDecimal(companyRealTimeStat.getSafePayTransSuccessCount()).multiply(new BigDecimal("100")).divide(new BigDecimal(companyRealTimeStat.getSafePayTransCount()), 2, RoundingMode.HALF_UP).toString();
        }


        vo.setAverageRate(averageRate);
        vo.setPayTransSuccessRate(payTransSuccessRate);
        vo.setSafePaySuccessRate(safePaySuccessRate);

        vo.setUpdateTime(companyRealTimeStat.getUpdateTime());
        vo.setRegisterCount(companyRealTimeStat.getRegisterCount());
        vo.setServiceChargeFee(companyRealTimeStat.getServiceChargeFee());
        vo.setTotalCount(companyRealTimeStat.getTotalCount());
        vo.setTotalSuccessCount(companyRealTimeStat.getTotalSuccessCount());
        vo.setTotalFailCount(companyRealTimeStat.getTotalFailCount());
        vo.setTotalSuccessSingleVeryCount(companyRealTimeStat.getTotalSuccessSingleVeryCount());
        vo.setTotalFailSingleVeryCount(companyRealTimeStat.getTotalFailSingleVeryCount());
        vo.setTotalServicePrice(companyRealTimeStat.getTotalServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalTransAmount(companyRealTimeStat.getTotalTransAmount());
        vo.setTotalSuccessTransAmount(companyRealTimeStat.getTotalSuccessTransAmount());
        vo.setTotalFailTransAmount(companyRealTimeStat.getTotalFailTransAmount());
        vo.setTotalBenefit(companyRealTimeStat.getTotalBenefit());
        vo.setPayTransCount(companyRealTimeStat.getPayTransCount());
        vo.setPayTransSuccessCount(companyRealTimeStat.getPayTransSuccessCount());
        vo.setPayTransFailCount(companyRealTimeStat.getPayTransFailCount());

        vo.setPayTransSuccessSingleVeryCount(companyRealTimeStat.getPayTransSuccessSingleVeryCount());
        vo.setPayTransFailSingleVeryCount(companyRealTimeStat.getPayTransFailSingleVeryCount());
        vo.setPayTransAmount(companyRealTimeStat.getPayTransAmount());
        vo.setPayTransSuccessAmount(companyRealTimeStat.getPayTransSuccessAmount());
        vo.setPayTransFailAmount(companyRealTimeStat.getPayTransFailAmount());
        vo.setPayTransServicePrice(companyRealTimeStat.getPayTransServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalPayTransBenefit(companyRealTimeStat.getTotalPayTransBenefit());
        vo.setSafePayTransCount(companyRealTimeStat.getSafePayTransCount());
        vo.setSafePayTransSuccessCount(companyRealTimeStat.getSafePayTransSuccessCount());
        vo.setSafePayTransFailCount(companyRealTimeStat.getSafePayTransFailCount());
        vo.setSafePayTransVeryAlipayUserCount(companyRealTimeStat.getSafePayTransVeryAlipayUserCount());

        vo.setSafePayTransFailSingleVeryCount(companyRealTimeStat.getSafePayTransFailSingleVeryCount());
        vo.setSafePayTransAmount(companyRealTimeStat.getSafePayTransAmount());
        vo.setSafePayTransSuccessAmount(companyRealTimeStat.getSafePayTransSuccessAmount());
        vo.setSafePayTransFailAmount(companyRealTimeStat.getSafePayTransFailAmount());
        vo.setSafePayTransServicePrice(companyRealTimeStat.getSafePayTransServicePrice().multiply(new BigDecimal("0.01")).setScale(2, RoundingMode.HALF_UP));
        vo.setTotalSafePayTransBenefit(companyRealTimeStat.getTotalSafePayTransBenefit());


        vo.setPayTransActiveUserCount(companyRealTimeStat.getPayTransActiveUserCount());
        vo.setPayTransActivePartnerCount(companyRealTimeStat.getPayTransActivePartnerCount());

        vo.setSafePayActiveUserCount(companyRealTimeStat.getSafePayActiveUserCount());
        vo.setSafePayActivePartnerCount(companyRealTimeStat.getSafePayActivePartnerCount());

        vo.setSafePayPayerAlipayAccountCount(companyRealTimeStat.getSafePayPayerAlipayAccountCount());
        vo.setSafePayPayeeAlipayAccountCount(companyRealTimeStat.getSafePayPayeeAlipayAccountCount());

        vo.setPayTransPayerAlipayAccountCount(companyRealTimeStat.getPayTransPayerAlipayAccountCount());
        vo.setPayTransPayeeAlipayAccountCount(companyRealTimeStat.getPayTransPayeeAlipayAccountCount());

        vo.setSafePayTransSuccessSingleVeryCount(companyRealTimeStat.getSafePayTransSuccessSingleVeryCount());
        vo.setPayTransVeryAlipayUserCount(companyRealTimeStat.getPayTransVeryAlipayUserCount());

        vo.setTotalTransBankAmount(companyRealTimeStat.getTotalTransBankAmount());
        vo.setTotalTransAlipayAmount(companyRealTimeStat.getTotalTransAlipayAmount());
        vo.setTotalTransAlipayAmount(companyRealTimeStat.getTotalTransAlipayAmount());
        vo.setTotalSuccessTransAlipayAmount(companyRealTimeStat.getTotalSuccessTransAlipayAmount());

        vo.setSafePayTransBankAmount(companyRealTimeStat.getSafePayTransBankAmount());
        vo.setSafePayTransAlipayAmount(companyRealTimeStat.getSafePayTransAlipayAmount());
        vo.setSafePayTransSuccessBankAmount(companyRealTimeStat.getSafePayTransSuccessBankAmount());
        vo.setSafePayTransSuccessAlipayAmount(companyRealTimeStat.getSafePayTransSuccessAlipayAmount());
        return new ResultData<>(vo);
    }
}
